<template>
    <div class="evaluate">
        <van-nav-bar title="发表评论" left-text="返回" left-arrow @click-left="onClickLeft">
            <template #right>
                <van-button @click="onSubmit">发布</van-button>
            </template>
        </van-nav-bar>
        <div class="box">
            <van-notice-bar color="#1989fa" background="#ecf9ff" left-icon="info-o">
            客观真实的评价能帮助用户做决策
        </van-notice-bar>
        <div class="top">
            <img style="width:4rem" :src="cover" alt="">
            <span id="name">{{infoname}}</span>
        </div>
        <van-form>
            <van-field name="rate" label="评分">
                <template #input>
                    <van-rate v-model="formObj.rate" />
                </template>
            </van-field>
            <van-field v-model="formObj.message" rows="2" autosize type="textarea" maxlength="50" placeholder="从多个角度评价宝贝，可以帮助更多想买的人"
                show-word-limit />
        </van-form>
        </div>
        
    </div>
</template>
<script>
export default {
    data() {
        return {
            formObj:{ rate: 0,message:""},
            cover:'',
            infoname:''
        }
    },
    methods: {
        onClickLeft() {
            this.$router.back()
        },
        async onSubmit() {
            this.formObj.infoname=this.infoname;
            this.formObj.order_id=this.$route.query.order_id;
            this.formObj.type=this.$route.query.type;
            this.formObj.id=this.$route.query.id;
            let res=await this.$frontaxios.post(this.$front.postEvaluateSubmit,this.formObj)
            if(res.code==1){
                this.$toast.success('谢谢您，添加评价成功!');
            }else if(res.code==-2){
                this.$toast.fail('该订单已经评论过了!');
            }else{
                this.$toast.fail('对不起，添加评价失败!');
            }
        },
    },
    async created(){
       let type=this.$route.query.type;
       let id=this.$route.query.id;
       if(type=='column'){
          let res=await this.$frontaxios.post(this.$front.getOneColumn2,{id});
          if(res.code==1){
             this.infoname=res.data.title;
             this.cover=res.data.cover;
          }else{
              this.$toast.fail('获取商品信息失败');
          }
       }else if(type=='exam'){
          let res=await this.$frontaxios.post(this.$front.getOneExamInfo,{id});
          if(res.code==1){
             this.infoname=res.data.title;
             this.cover=res.data.cover;
          }else{
              this.$toast.fail('获取商品信息失败');
          }
       }else{
          let res=await this.$frontaxios.post(this.$front.getOneKnowledgeInfo,{id});
          if(res.code==1){
            this.infoname=res.data.title;
            this.cover=res.data.cover;
          }else{
              this.$toast.fail('获取商品信息失败');
          }
       }
    }
}
</script>
<style scoped>
.van-button {
    height: 50%;
    background-color: #EE8C33;
    border-radius: 1rem;
    color: #FFF;
}

.top {
    display: flex;
}

#name {
    margin: 1rem;
}
.evaluate{
    background-color:#F6F6F6 ;
    height: 100%;
}
.box{
    background-color: #FFF;
}
</style>
